package model;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class TareaImprimirPrograNumber implements Estrategia {

	private Map map = new HashMap();
	private Parser parser;

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public Parser getParser() {
		return parser;
	}

	public void setParser(Parser parser) {
		this.parser = parser;
	}

	@Override
	public void actuar(Packet packet) {

		if (packet.getPid() == 0x00) {


			int s_len = (calcularS_Len(packet.getPayload()[2],
					packet.getPayload()[3]) - 9) / 4;
			int j = 9;
			for (int i = 0; i < s_len; i++) {

				int k = ((packet.getPayload()[j] & 0x00FF) << 8)
						| (packet.getPayload()[j + 1] & 0x00FF);

				int v = parser.pid(packet.getPayload()[j + 2],
						packet.getPayload()[j + 3]);
				j = j+4;
				map.put(k, v);
			}
		}
	}

	private int calcularS_Len(int i, int j) {
		return (((i & 0x0F) << 8) | j);
	}

	public void imprimir(FileInputStream file) {
		parser.parser(file);
		 Iterator it = map.keySet().iterator();
		//System.out.print(map);
		 while(it.hasNext()){
		 Integer key = (Integer) it.next();
	String k = String.format("%04x",  key);
	String v = String.format("%04x",  map.get(key));
	
		 System.out.println("Clave: " + k + " -> Valor: " + v);
		 }
	}

	public static void main(String[] arg) throws FileNotFoundException {
		Parser p = new Parser();
		TareaImprimirPrograNumber t = new TareaImprimirPrograNumber();
		FileInputStream file = new FileInputStream(arg[0]);
		p.setEstrategia(t);
		t.setParser(p);
		t.imprimir(file);
		t.getMap();
	}
}
